package space.misiro.ledgers.token.exchange

import com.fasterxml.jackson.annotation.JsonProperty
import org.keycloak.common.util.Time.currentTime
import kotlin.math.min

data class TokenConfiguration(
    @field:JsonProperty("tokenLifespanInSeconds")
    val tokenLifespanInSeconds: Int,

    @field:JsonProperty("scope")
    val scope: String
) {

    fun computeTokenExpiration(maxExpiration: Int, longLivedTokenAllowed: Boolean) =
        (tokenLifespanInSeconds + currentTime()).let { it.takeIf { longLivedTokenAllowed } ?: min(maxExpiration, it) }
}